﻿using Data;
using PayrollExpress.Helpers;
using PayrollExpress.Models;
using System;
using System.Data.Entity;
using System.Web.Mvc;

namespace PayrollExpress.Controllers
{
    public class WorkspaceController : BaseController
    {

        public ActionResult Index()
        {
            try
            {
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        #region Payroll

        public ActionResult Payroll(int? workLocationID)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Payroll);

                var model = new PayrollListModel();
                using (var db = new DatabaseContext())
                {
                    model = WorkspaceHelper.GetPayrollListModel(db, User.Identity.Name, workLocationID);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        [HttpPost]
        public ActionResult PayrollRefresh(PayrollListModel model)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Payroll);

                return RedirectToAction(BaseHelper.Page.Payroll.ToString(), new { workLocationID = model.SelectedWorkLocationID });
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult PayrollAddEdit(int? workLocationID, int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.Payroll);

                var model = new PayrollAddEditModel();
                using (var db = new DatabaseContext())
                {
                    model = WorkspaceHelper.GetPayrollAddEditModel(db, User.Identity.Name, workLocationID, id);
                }

                return View(BaseHelper.PageAddEditSave.PayrollAddEdit.ToString(), model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult PayrollSave(PayrollAddEditModel model, string command)
        {
            try
            {
                using (var db = new DatabaseContext())
                {
                    if (command == BaseHelper.PageAction.Add.ToString())
                    {
                        db.Payrolls.Add(model.Type);
                        db.SaveChanges();
                    }
                    else if (command == BaseHelper.PageAction.Edit.ToString())
                    {
                        db.Entry(model.Type).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        // cancelled
                    }
                }

                return RedirectToAction(BaseHelper.Page.Payroll.ToString(), new { workLocationID = model.Type.WorkLocationID });
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        #endregion

        #region Payroll Details

        public ActionResult PayrollDetails(int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.PayrollDetails);

                var model = new PayrollDetailListModel();
                using (var db = new DatabaseContext())
                {
                    model = WorkspaceHelper.GetPayrollDetailListModel(db, id.GetValueOrDefault(), hasDetails: true);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult GeneratePayrollDetails(int? id)
        {
            try
            {
                CheckUserPageAccess(BaseHelper.Page.PayrollDetails);

                var model = new PayrollDetailListModel();
                using (var db = new DatabaseContext())
                {
                    model = WorkspaceHelper.GetPayrollDetailListModel(db, id.GetValueOrDefault(), hasDetails: false);
                }

                return View(model);
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        #endregion

    }
}
